package com.qianyucc.blog.controller.comm;

import com.qianyucc.blog.mapper.*;
import com.qianyucc.blog.model.vo.*;
import com.qianyucc.blog.service.*;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.data.redis.core.*;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.qianyucc.blog.global.Constants.DEFAULT_PAGE_SIZE;

/**
 * @author lijing
 * @date 2019-10-12 14:49
 * @description 与文章相关的api
 */
@RestController("commArticleController")
@RequestMapping("comm/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation("根据条件分页显示文章")
    @GetMapping("/page")
    public PageInfoVO<SimpleArticleVO> getPageArticles(
            @ApiParam("页码") @RequestParam(value = "pageNumber", defaultValue = "1", required = false) Integer pageNumber,
            @ApiParam("约束条件") @RequestParam(required = false) String condition,
            @ApiParam("约束条件对应的信息") @RequestParam(required = false) String data
    ) {
        return articleService.findByCondition(pageNumber, DEFAULT_PAGE_SIZE, condition, data);
    }


    @ApiOperation("根据文章id获取文章信息")
    @GetMapping("/{id}")
    public ArticleVO getArticleById(@PathVariable String id) {
        return articleService.findArticleById(id);
    }

    @ApiOperation("获取热门文章列表")
    @GetMapping(value = "/hot", produces = "application/json;charset=UTF-8")
    public String getHotArticles() {
        return redisTemplate.opsForValue().get("hotArticles");
    }

    @ApiOperation("获取所有文章分类")
    @GetMapping("/categories")
    public List<Map<String, Object>> getCategories() {
        return articleMapper.findCategoriesCount();
    }

    @ApiOperation("获取所有文章标签")
    @GetMapping("/tags")
    public Set<String> getTags() {
        Set<String> tags = articleService.findAllTags();
        return tags;
    }

    @ApiOperation("获取文章的所有月份")
    @GetMapping("/archives")
    public Set<String> getArchivesMonths() {
        Set<String> months = articleService.archivesMonths();
        return months;
    }
}
